package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.databinding.ActivityListBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class ListActivity extends BaseAc<ActivityListBinding> {

    public static int type;//0最热 1最新 2高分
    private RecommendAdapter hotAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_list;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivListBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.llHot.setOnClickListener(this);
        mDataBinding.llLatest.setOnClickListener(this);
        mDataBinding.llClassic.setOnClickListener(this);
        mDataBinding.rvHotList.setLayoutManager(new GridLayoutManager(mContext, 2));
        hotAdapter = new RecommendAdapter();
        hotAdapter.setShow(true);
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llHot:
                clearSelection();
                page = 1;
                getHotData();
                mDataBinding.ivHot.setImageResource(R.drawable.icon_jt1);
                break;
            case R.id.llLatest:
                clearSelection();
                page = 1;
                getLatestData();
                mDataBinding.ivLatest.setImageResource(R.drawable.icon_jt1);
                break;
            case R.id.llClassic:
                clearSelection();
                page = 1;
                getClassicData();
                mDataBinding.ivClassic.setImageResource(R.drawable.icon_jt1);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        saveRecord(bean);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        if (type == 0) {
            clearSelection();
            getHotData();
            mDataBinding.ivHot.setImageResource(R.drawable.icon_jt1);
        } else if (type == 1) {
            clearSelection();
            getLatestData();
            mDataBinding.ivLatest.setImageResource(R.drawable.icon_jt1);
        } else if (type == 2) {
            clearSelection();
            getClassicData();
            mDataBinding.ivClassic.setImageResource(R.drawable.icon_jt1);
        }
        mDataBinding.refreshTabItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshTabItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshTabItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (type == 0) {
                    getHotData();
                } else if (type == 1) {
                    getLatestData();
                } else if (type == 2) {
                    getClassicData();
                }
                mDataBinding.refreshTabItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (type == 0) {
                    getHotData();
                } else if (type == 1) {
                    getLatestData();
                } else if (type == 2) {
                    getClassicData();
                }
                mDataBinding.refreshTabItem.finishRefresh(refreshTime);
            }
        });
    }

    private void getHotData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            hotAdapter.setList(result);
                        } else {
                            hotAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getLatestData() {
        StkResApi.getTagResourceList(this, AppConfig.LATEST_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            hotAdapter.setList(result);
                        } else {
                            hotAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getClassicData() {
        StkResApi.getTagResourceList(this, AppConfig.CLASSIC_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            hotAdapter.setList(result);
                        } else {
                            hotAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void clearSelection() {
        page = 1;
        mDataBinding.ivHot.setImageResource(R.drawable.icon_jt2);
        mDataBinding.ivLatest.setImageResource(R.drawable.icon_jt2);
        mDataBinding.ivClassic.setImageResource(R.drawable.icon_jt2);
    }

    private void saveRecord(StkResBean bean) {
        List<StkResBean> listSave = new ArrayList<>();
        listSave.add(bean);
        List<StkResBean> listSp = AppSpUtils.getReadRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveReadRecordList(listSp);
        } else {
            AppSpUtils.saveReadRecordList(listSave);
        }
    }

}